package com.pzhu.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.pzhu.model.entity.Bookable;
import com.pzhu.model.vo.BookableVo;
import com.pzhu.model.vo.BookableVoPlus;
import com.pzhu.model.vo.DatePeriod;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zengqiuhu
 * @date 2023/9/11 17:21
 */

public class ToolUtils {

    public static void main(String[] args) {
        System.out.println(buildVipCode());
    }

    /**
     * 构建VIP的code
     * @return
     */
    public static String buildVipCode(){
        long currentTime = System.currentTimeMillis();
        int year = Calendar.getInstance().get(Calendar.YEAR);
        return "MM"+year+ currentTime % 1000000;
    }

    /**
     * 处理预约时间 时间格式yyyy-MM-dd HH:mm:ss
     */
    public static List<BookableVoPlus> handleBookableTime(List<Bookable> bookableList,Integer area){

        List<BookableVo> bookableVos = BeanCopyUtils.copyBeanList(bookableList, BookableVo.class);

        //首先过滤面积大小计算的时间
        long duration = 0L ;
        if (Objects.nonNull(area)){
            duration = Double.valueOf((area / 25.0)*3600000).longValue();
        }

        final long finalDuration = duration;

        //判断时间的持续时间是否满足条件
        List<BookableVo> bookableVoList = bookableVos.stream().filter(bookable ->
                bookable.getEndTime().getTime() - bookable.getStartTime().getTime() >= finalDuration
        ).collect(Collectors.toList());


        DateFormat date = new SimpleDateFormat("yyyy-MM-dd");

        //修改一些字段格式
        bookableVoList.forEach(v->{
            v.setDate(date.format(v.getStartTime()));
        });

        //通过年月日分组
        Map<String, List<BookableVo>> personMap =
                bookableVoList.stream().collect(Collectors.groupingBy(BookableVo::getDate));

        System.out.println(personMap);

        //组装最后返回的结果
        DateFormat time = new SimpleDateFormat("HH:mm");

        Calendar calendar = Calendar.getInstance();

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        List<BookableVoPlus> list = new ArrayList<>();

        for(Map.Entry<String,List<BookableVo>> entry : personMap.entrySet()){

            BookableVoPlus bookableVoPlus = new BookableVoPlus();

            bookableVoPlus.setDate(entry.getKey());

            try {
                calendar.setTime(dateFormat.parse(entry.getKey()));  // 设置Calendar对象为该日期
            } catch (ParseException e) {
                e.printStackTrace();
            }
            //设置周几
            bookableVoPlus.setWeek(getWeekdayString(calendar.get(Calendar.DAY_OF_WEEK)));

            List<BookableVo> value = entry.getValue();

            //时间段集合
            List<DatePeriod> periodList = new ArrayList<>();

            for (BookableVo bookableVo : value) {

                DatePeriod period = new DatePeriod();

                period.setId(bookableVo.getId());

                period.setStartTime(time.format(bookableVo.getStartTime()));
                period.setEndTime(time.format(bookableVo.getEndTime()));
                period.setState(bookableVo.getState());

                periodList.add(period);
            }
            bookableVoPlus.setDatePeriod(periodList);

            list.add(bookableVoPlus);
        }
        return  list;
    }

    /**
     * 生成订单号
     * @return
     */
    public static String getCode(){
        String format = DateUtil.format(new Date(),"yyyyMMddHHmmss");
        String numbers = RandomUtil.randomNumbers(5);
        return format+numbers;
    }

    /**
     * 获取星期的字符串表示
     * @param weekday 星期数字表示
     * @return 星期的字符串表示
     */
    private static String getWeekdayString(int weekday) {
        switch (weekday) {
            case Calendar.SUNDAY:
                return "周天";
            case Calendar.MONDAY:
                return "周一";
            case Calendar.TUESDAY:
                return "周二";
            case Calendar.WEDNESDAY:
                return "周三";
            case Calendar.THURSDAY:
                return "周四";
            case Calendar.FRIDAY:
                return "周五";
            case Calendar.SATURDAY:
                return "周六";
            default:
                throw new IllegalArgumentException("Invalid weekday value: " + weekday);
        }
    }

}
